var lionsApp = angular.module('lionsApp', ['ui.router', 'ngResource'])
lionsApp.constant('httpConfig', {
    api: '',
    headers: {
        'content-type': 'application/json'
    }
})
lionsApp.run(['$location', '$rootScope', function($location, $rootScope){
    $rootScope.$on('$stateChangeSuccess', function (event, currentRoute) {
        //update title hack for ios webview
        var iframe = document.createElement('iframe')
        if (currentRoute.title) $rootScope.pageTitle = currentRoute.title
        iframe.src = './favicon.ico'
        iframe.style.display = 'none'
        iframe.onload = function () {
            setTimeout(function () {
                document.body.removeChild(iframe)
            }, 10)
        }
        document.body.appendChild(iframe)
    })
}])
//七牛图片输出
lionsApp.filter('resetImg', function () {
    return function (val) {
        if (val) {
            return val + '?imageView2/2/w/300/q/80'
        }
    }
})
lionsApp.filter('resetImgBig', function () {
    return function (val) {
        if (val) {
            return val + '?imageView2/2/w/750/q/80'
        }
    }
})
//顶部菜单
lionsApp.directive('top', function () {
    return {
        restrict: 'E',
        templateUrl: './templates/directive/top.ng',
        replace: true
    }
})
//FOOTER
lionsApp.directive('bottom', function () {
    return {
        restrict: 'E',
        templateUrl: './templates/directive/bottom.ng',
        replace: true,
        link: function (scope) {
            scope.showMenu = function () {
                scope.isShow = !scope.isShow
            }
        }
    }
})
//解决click 300ms延迟用touchstart事件替换click事件
lionsApp.directive('ngTouchstart', function () {
    return {
        controller: ['$scope', '$element', function ($scope, $element) {
            $element.bind('touchstart', onTouchStart)
            function onTouchStart() {
                var method = $element.attr('ng-touchstart')
                $scope.$apply(method)
            }
        }]
    }
})
//countdown
lionsApp.directive('countdown', function ($interval) {
    return {
        restrict: 'E',
        templateUrl: './templates/directive/countdown.ng',
        replace: true,
        scope: {
            times: '@times'
        },
        link: function (scope) {
            scope.countDown = function () {
                if(scope.times <= 0){
                    $interval.cancel(scope.timer)
                }
                scope.days = parseInt(scope.times/(3600*24))
                scope.hours = parseInt((scope.times - scope.days*24*3600)/3600)
                scope.minutes = parseInt((scope.times - scope.days*24*3600 - scope.hours*3600)/60)
                scope.seconds = scope.times - scope.days*24*3600 - scope.hours*3600 - scope.minutes*60
                scope.times--
            }
            scope.countDown()
            scope.timer = $interval(scope.countDown, 1000)
        }
    }
})
lionsApp.factory('myFunction', function () {
    return {
        getUrlParam: function (val) {
            if(val.indexOf('#') == -1 || val.indexOf('?') == -1){
                return false
            }else{
                var url = val.split('#')[1]
                var urlParam = url.split('?')[1]
                var urlObj = {}
                urlParam.split('&').forEach( function (value) {
                    urlObj[value.split('=')[0]] = value.split('=')[1]
                })
                return urlObj
            }
        }
    }
})
//拍卖状态
lionsApp.filter('auctionStatus', function () {
    return function (val) {
        switch (val) {
            case 10:
                return '出价被超'
                break
            case 20:
                return '出价最高'
                break
            case 30:
                return '未支付'
                break
            case 40:
                return '明拍商品'
                break
            case 50:
                return '已支付'
        }
    }
})